(function($){
	var self;
	WdListManager = function(url)
	{
		self = this;
		this.url = null;
		this.del_url = null;
		this.page = null;
		this.listsize = null;
		this.pagesize = null;
		this.pagebar = null;
		this.json=null;
		this.root=null;
		this.totalbox=null;
		this.tr_effect=true;
		this.params = {};
		this.init(url);
	};
	WdListManager.prototype.init = function(url)
	{
		this.set_url(url+'json');
		this.set_del_url(url+'del');
		this.set_page(1);
	};
	WdListManager.prototype.set_url			=	function(url)			{this.url = url;};
	WdListManager.prototype.set_del_url		=	function(url)			{this.del_url = url;};
	WdListManager.prototype.set_pagebar		=	function(pagebar)		{this.pagebar = pagebar;};
	WdListManager.prototype.set_root		=	function(root)			{this.root = root;};
	WdListManager.prototype.set_totalbox	=	function(totalbox)		{this.totalbox = totalbox;};
	WdListManager.prototype.set_tr_effect	=	function(tr_effect)		{this.tr_effect = tr_effect;};

	WdListManager.prototype.set_page = function(page)
	{
		if(page == '' || page == '<<' || page == '<' || page == '>' || page == '>>') return;
		this.page = page;
		this.set_param('page',this.page);
	};
	WdListManager.prototype.set_param = function(key, value)
	{
		if(value == '')
		{
			delete this.params[key];
			return;
		}
		this.params[key] = value;
	};

	WdListManager.prototype.get = function(page)
	{ 
		this.set_page(page);
		$(self.root).mask('Loading..');
		$.post(	self.url
				,self.params
				,function(json){
					self.json = json;
					var tbody = $(self.root).children('tbody');
					$(self.root+' input.checkAll').attr('checked',false);
					if(json.error)
					{
						$(self.root).unmask();
						alert(json.error);
					}
					else
					{
						tbody.empty();
						if(json.lists.length == 0)
						{
							$(tbody).append('<tr><td class="nolist" colspan="100">'+self.empty()+'</td></tr>');
						}
						else
						{
							$.each(json.lists, function(){
								$(tbody).append('<tr>'+self.draw(this)+'</tr>');
							});
							if(self.tr_effect == true)
							{
								tbody.children('tr').map(function(){
									$(this).children('td:odd').addClass('odd');
									$(this).hover(function(){$(this).addClass('hover');}, function(){$(this).removeClass('hover');});
								});	
							}
							self.draw_event();
						}
						if(self.totalbox != null)
						{
							$(self.totalbox).text(new String(self.json.total).replace(/(\d)(?=(?:\d{3})+(?!\d))/g,'$1,'));
						}
						self.paging();
						$(self.root).unmask();
					}
				},'json'
		);
	};
	WdListManager.prototype.preload = function()
	{ 
		this.set_page(1);
		this.search_event();
		$(self.root).mask('Loading..');
		
		var tbody = $(self.root).children('tbody');
		var json = eval('(' + tbody.attr('data') + ')');
	
		if(json.error)
		{
			$(self.root).unmask();
		}
		else
		{
			self.json = json;
			self.listsize = json.listsize;
			self.pagesize = json.pagesize;
			if(json.lists.length == 0)
			{
				$(tbody).append('<tr><td class="nolist" colspan="100">'+self.empty()+'</td></tr>');
			}
			else
			{
				$.each(json.lists, function(){
					$(tbody).append('<tr>'+self.draw(this)+'</tr>');
				});
				if(self.tr_effect == true)
				{
					tbody.children('tr').map(function(){
						$(this).children('td:odd').addClass('odd');
						$(this).hover(function(){$(this).addClass('hover');}, function(){$(this).removeClass('hover');});
					});	
				}
				this.draw_event();
			}
			if(self.totalbox != null)
			{
				$(self.totalbox).text(new String(self.json.total).replace(/(\d)(?=(?:\d{3})+(?!\d))/g,'$1,'));
			}
			this.paging();
			$(self.root).unmask();
		}
	};
	WdListManager.prototype.reload	= function()
	{
		this.get(this.page);
	};
	WdListManager.prototype.paging = function()
	{
		var nav = [];
		var start = Math.floor((this.page-1) / this.pagesize) * this.pagesize;
		var page_total = Math.ceil(this.json.total / this.listsize);
//		if(start > 0)
		{
			nav.push('<a href="javascript:;" class="first">처음</a>');
			nav.push('<a href="javascript:;" class="prev">이전</a>');
		}
		if(start == 0)
		{
			nav.push('<a href="javascript:;" class="on">1</a>');
		}
		else
		{
			for(var i=1; i <= this.pagesize; i++)
			{
				var p = start+i;
				if(p > page_total) continue;
				if(p == this.page)	nav.push('<a href="javascript:;" class="on">'+p+'</a>');
				else			nav.push('<a href="javascript:;">'+p+'</a>');
			}
		}
//		if((p+1) <= page_total)
		{
			nav.push('<a href="javascript:;" class="next">다음</a>');
			nav.push('<a href="javascript:;" class="last">마지막</a>');
		}

		$(this.pagebar).html(nav.join(' '));
		$('a', this.pagebar).click(function(){
			if($(this).hasClass('first'))
			{
				if(self.page == 1) return;
				self.page = 1;
			}
			else if($(this).hasClass('prev'))
			{
				if(self.page == 1) return;
				self.page--;
			}
			else if($(this).hasClass('next'))
			{
				if(self.page == page_total) return;
				self.page++;
			}
			else if($(this).hasClass('last'))
			{
				if(self.page == page_total) return;
				self.page = page_total;
			}
			else 
			{
				return;
			}
			self.get(self.page);
		});
		$(this.pagebar+' a').focus(function(){$(this).blur();});
	};
	WdListManager.prototype.draw		=	function(json){alert('need to implement...');};
	WdListManager.prototype.draw_event	=	function(){};
	WdListManager.prototype.deleting	=	function(){};

	

	WdListManager.prototype.empty = function()
	{
		return '데이터가 존재하지 않습니다.';
	};
	
	WdListManager.prototype.search_event = function()
	{
		if($('#searchForm').length == 1)
		{
			$('#searchForm .btn').click(function(){
				$($('#searchForm').serializeArray()).map(function(){
					self.set_param(this.name, this.value);
				});
				if($('#searchForm :text[name=sk]').length == 1)
				{
					if($('#searchForm :text[name=sk]').val() == '')
					{
						alert('검색어를 입력하십시오.');
						$('#searchForm :text[name=sk]').addClass('error');
						return $('#searchForm :text[name=sk]').focus();
					}
					$('#searchForm :text[name=sk]').removeClass('error');
				}
				self.reload();
			});
		}
	};
	
	WdListManager.prototype.del = function(params)
	{
		var checked = $(self.root+' tbody :checked');
		if(checked.length == 0)
		{
			return alert('삭제할 목록을 선택하십시오.');
		}
		
		if(confirm('선택한 '+checked.length+'개의 목록을 삭제하시겠습니까?'))
		{
			var nWde = null;
			if(params == undefined) params = {};
			params['del_seq'] = checked.map(function(){return this.value;}).get();
			$.post(this.del_url, params, function(res){
				if(res.error == true)
				{
					return alert('에러가 발생했습니다.');
				}
				
				self.reload();
			});
		}
	};
	$(document).ready(function(){
		$(':checkbox.checkAll').click(function(){
			var checked = this.checked;
			$(this).parent().parent().parent().next().find(':checkbox').map(function(){
				this.checked = checked;
			});
		});
		
		$(':checkbox').map(function(){
			this.checked = false;
		});
	});
})(jQuery);
